/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mycompany.psc.controllers;

import com.mycompany.psc.ejb.DepartmentsManager;
import com.mycompany.psc.entities.Department;
import com.mycompany.psc.entities.Users;
import com.mycompany.psc.producers.LoggedIn;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

/**
 *
 * @author A7med
 */
@Named
@SessionScoped
public class DepartmentsController implements Serializable{
    @EJB
    DepartmentsManager dm;
    private Department selected;
    private String departName;
    private String departDesc;
    private Department deptParent;
    private Set<Department>readSet;
    private Set<Department>writeSet;
    @Inject @LoggedIn
    Users current;    

    public Set<Department> getReadSet() {
        return readSet;
    }

    public void setReadSet(Set<Department> readSet) {
        this.readSet = readSet;
    }

    public Set<Department> getWriteSet() {
        return writeSet;
    }

    public void setWriteSet(Set<Department> writeSet) {
        this.writeSet = writeSet;
    }
    
    

    /**
     * @return the selected
     */
    public Department getSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    public void setSelected(Department selected) {
        this.selected = selected;
    }

    /**
     * @return the departName
     */
    public String getDepartName() {
        return departName;
    }

    /**
     * @param departName the departName to set
     */
    public void setDepartName(String departName) {
        this.departName = departName;
    }

    /**
     * @return the departDesc
     */
    public String getDepartDesc() {
        return departDesc;
    }

    /**
     * @param departDesc the departDesc to set
     */
    public void setDepartDesc(String departDesc) {
        this.departDesc = departDesc;
    }

    /**
     * @return the deptParent
     */
    public Department getDeptParent() {
        return deptParent;
    }

    /**
     * @param deptParent the deptParent to set
     */
    public void setDeptParent(Department deptParent) {
        this.deptParent = deptParent;
    }
    public List<Department>getDeparts(){
        List<Department> set = dm.getChildren(0);
        List<Department>removables=new ArrayList<>();
        for(Department d:set){
            if(!current.canRead(d)||d.getName().equalsIgnoreCase("admin")){
                removables.add(d);
            }
        }
        set.removeAll(removables);
        return set;
    }
     public List<Department>getAllUsersDeparts(){
        List<Department> set = dm.getChildren(0);
        
        return set;
    }
     public List<Department>getDepartsChildren(int id){
        return dm.getChildren(id);
    }
    public List<Department>getAllDeparts(){
        return dm.getAll();
    }
    public void saveDept(){
        System.out.println("saving"+departDesc);
        dm.save(departName, departDesc, readSet, writeSet, 0);
    }
     public void saveDeptChanges(){
        System.out.println("saving"+departDesc);
        dm.saveChanges(selected);
    }
    public void saveSection(){
        System.out.println("saving"+deptParent);
        dm.save(departName, departDesc, null, null, deptParent.getId());
    }
    public TreeNode sectionsTree(){
        TreeNode root=new DefaultTreeNode("Departments overview", null);
        for(Department parent:getAllUsersDeparts()){
            TreeNode root1=new DefaultTreeNode(parent, root);
            addChildren(parent,root1);
        }
        return root;
    }
    public void onNodeSelect(NodeSelectEvent event) {
       deptParent=(Department) event.getTreeNode().getData();
        System.out.println("scder"+deptParent.getName());
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "selected", deptParent.toString());

        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    private void addChildren(Department parent, TreeNode root1) {
       
        for (Department child : dm.getChildren(parent.getId())) {
            TreeNode node0 = new DefaultTreeNode(child, root1);
             
                addChildren(child,node0);
            
        }
        
    }
    public void deleteDepartment(){
        
        dm.delete(selected);
    }
    public void deleteSection(){
        
        dm.delete(deptParent);
    }
}
